#!/usr/bin/perl

# compress tool
package Compress;

use File;

$this = {};

sub new {
    my $class = shift;
    my $config = shift;

    $this = {
        _imageMagickDir => $config->{imageMagickDir}
    };

    bless $this, $class;
}

sub imageMagickDir {
    $this->{_imageMagickDir};
}

sub png2jpg{

    shift;
    my $png = shift;
    unless($png) {
        die "Illegal argument : $png \n";
    }

    my $pngFile = File->new($png);
    my $pngSuffix = $pngFile->suffix;
    unless($pngSuffix){
        die "Illegal file type, suffix = $pngSuffix \n";
    }
    
    my $pngFullPath = $pngFile->fullPath;
    my $jpgFullPath = $pngFile->basePath . '/' . $pngFile->filename. '.jpg';
    
    _imageMagick_png2jpg($pngFullPath, $jpgFullPath);

    my $jpgFile = File->new($jpgFullPath);
    {filename => $jpgFile->filename . '.' . $jpgFile->suffix, size => $jpgFile->size};
}

sub _imageMagick_png2jpg {
    my $dir = $this->{_imageMagickDir};
    unless($dir){
        die "Compress error, imageMagick dir is empty! \n";
    }

    my $pngFullPath = shift;
    my $jpgFullPath = shift;

    system("$dir/convert -format jpg $pngFullPath $jpgFullPath");
}

1;
